package com.egg.ylt.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.GlideRoundTransform;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.activity.ACT_ShopAppointment;
import com.egg.ylt.pojo.AppointmentServeListEntity;
import com.ruffian.library.widget.RTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/9/4.
 */

public class ADA_OrderList extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private int normalType = 0;     // 第一种ViewType，正常的item
    private int footType = 1;       // 第二种ViewType，底部的提示View
    private Context mContext;
    private LayoutInflater mInflater;
    private List<AppointmentServeListEntity.ListEntity> mList;
    private OnItemClickListener itemClickListener;
    private int listSize = 0;

    public ADA_OrderList(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    //添加数据
    public void addList(List<AppointmentServeListEntity.ListEntity> list, int listSize) {
        if (mList != null) {
            mList.clear();
        }
        this.listSize = listSize;
        mList.addAll(list);
        notifyDataSetChanged();
    }

    //更新数据
    public void updateList(List<AppointmentServeListEntity.ListEntity> list, int listSize) {
        mList.addAll((mList.size() - 1), list);
        this.listSize = listSize;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        // 根据返回的ViewType，绑定不同的布局文件，这里只有两种
        if (viewType == normalType) {
            return new OrderViewHolder(mInflater.inflate(R.layout.item_order_recycler, parent, false));
        } else {
            return new FootViewHolder(mInflater.inflate(R.layout.load_foot_view, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof OrderViewHolder) {

            final AppointmentServeListEntity.ListEntity entity = mList.get(position);
            OrderViewHolder orderViewHolder = (OrderViewHolder) holder;
            orderViewHolder.mOrderTimeTv.setText("预约时间：" + entity.getAppointmentTime());
            Glide.with(mContext)
                    .load(entity.getLogoUrl())
                    .transform(new GlideRoundTransform(mContext))
                    .transition(new DrawableTransitionOptions().crossFade())
                    .placeholder(R.mipmap.ic_serve_default)
                    .into(orderViewHolder.ivImgOrderItem);
            orderViewHolder.mShopNameTv.setText(entity.getName());

            ((OrderViewHolder) holder).mShopAddressTv.setText(entity.getAddress());

            switch (entity.getStatus()) {
                case 0:
                    orderViewHolder.mOrderStatusTv.setText("已预约");
                    orderViewHolder.mBottomLl.setVisibility(View.GONE);
                    break;
                case 1:
                    orderViewHolder.mOrderStatusTv.setText("进行中");
                    orderViewHolder.mBottomLl.setVisibility(View.GONE);
                    break;
                case 2:
                    orderViewHolder.mOrderStatusTv.setText("已完成");
                    orderViewHolder.mBottomLl.setVisibility(View.GONE);
                    break;
                case 3:
                    orderViewHolder.mOrderStatusTv.setText("预约取消");
                    orderViewHolder.mBottomLl.setVisibility(View.VISIBLE);
                    orderViewHolder.mReserveAgainLl.setVisibility(View.VISIBLE);
                    orderViewHolder.mReserveAgainLl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, ACT_ShopAppointment.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("orderId", entity.getId());
                            bundle.putString("shopId", entity.getShopId());
                            bundle.putString("storePhone", entity.getFixPhone());
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                        }
                    });
                    break;
            }

            orderViewHolder.mPriceTv.setText(StringUtil.getBigDecimalPrice(entity.getCashCollection()));
            orderViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClickListener.onClick(entity);
                }
            });
        } else if (holder instanceof FootViewHolder) {
            if (listSize > 0 && listSize == 20) {
                ((FootViewHolder) holder).tvText.setText("正在加载更多...");
            } else if (listSize > 0 && listSize < 20) {
                ((FootViewHolder) holder).tvText.setText("没有更多数据了");
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    // 根据条目位置返回ViewType，以供onCreateViewHolder方法内获取不同的Holder
    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return footType;
        } else {
            return normalType;
        }
    }

    static class OrderViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.order_time_tv)
        TextView mOrderTimeTv;
        @BindView(R.id.order_item_pic_iv)
        ImageView ivImgOrderItem;
        @BindView(R.id.shop_name_tv)
        TextView mShopNameTv;
        @BindView(R.id.shop_address_tv)
        TextView mShopAddressTv;
        @BindView(R.id.status_tv)
        TextView mOrderStatusTv;

        @BindView(R.id.bottom_ll)
        LinearLayout mBottomLl;

        @BindView(R.id.reserve_again_ll)
        LinearLayout mReserveAgainLl;

        @BindView(R.id.price_ll)
        LinearLayout mPriceLl;

        @BindView(R.id.price_tv)
        TextView mPriceTv;


        public OrderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {
        private RTextView tvText;

        public FootViewHolder(View itemView) {
            super(itemView);
            tvText = itemView.findViewById(R.id.foot_tv_text);
        }
    }

    public interface OnItemClickListener {
        void onClick(AppointmentServeListEntity.ListEntity entity);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
